#ifndef IOMANAGER_H
#define IOMANAGER_H
#include "Common/Macro.h"
#include "Common/Output.h"
#include "Rendering/Engine.h"
#include "Rendering/Camera.h"
#include "Rendering/Viewport.h"

#include "GL/glfw.h"

class IOManager
{
public:
	int Init();
	inline int ErrorAndExit();
	inline int ErrorAndExit(const char* aStr);

	void RunEngine();
	void ProcessInputs();

private:
	Engine* mEngine;
	Camera* mCamera;		// ref
	Viewport* mViewport;

	bool IsSpaceAlreadyPressed;

private:
	inline bool ShouldExit();

	/************************************************************************/
	/*							Singleton                                   */
	/************************************************************************/
public:
	static inline IOManager* GetIOManager();
	static inline void DeleteIOManager();

private:
	virtual ~IOManager(void) 
	{
		delete mEngine;
		delete mViewport;
	}
	IOManager(void) : mEngine(new Engine()), mCamera(NULL), mViewport(new Viewport()){}
	static IOManager* mPtr;

};

//////////////////////////////////////////////////////////////////////////
inline bool IOManager::ShouldExit()
{
	return !(glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ));
}

//////////////////////////////////////////////////////////////////////////
inline int IOManager::ErrorAndExit()
{
	glfwTerminate();
	system("PAUSE");
	return EXIT_FAILURE;
}

//////////////////////////////////////////////////////////////////////////
inline int IOManager::ErrorAndExit(const char* aStr)
{
	Output::Print(aStr);
	glfwTerminate();
	system("PAUSE");
	return EXIT_FAILURE;
}

//////////////////////////////////////////////////////////////////////////
inline IOManager* IOManager::GetIOManager()
{
	if (mPtr == NULL)
	{
		mPtr = new IOManager();
	}
	return mPtr;
}

//////////////////////////////////////////////////////////////////////////
inline void IOManager::DeleteIOManager()
{
	// Close window and terminate GLFW
	glfwTerminate();
	if (mPtr != NULL)
	{
		delete mPtr;
	}
}

#endif // IOMANAGER_H
